define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function ($, undefined, Backend, Table, Form, Template) {

    var Controller = {
        index: function () {

            // 初始化表格参数配置
            Table.api.init();
            this.table.first();
            this.table.second();
        },

        table: {
            first: function () {
                // 表格1
                var table1 = $("#table1");
                table1.bootstrapTable({
                    url: 'bdl/bd/org/index',
                    toolbar: '#toolbar1',
                    sortName: 'id',
                    search: false,
                    commonSearch: false,
                    showToggle: false,
                    showExport: false,
                    columns: [
                        [
                            {field: 'id', title: 'ID'},
                            {field: 'fnumber', title: '编码', operate: 'LIKE'},
                            {field: 'fname', title: '名称', operate: 'LIKE'},
                            {field: 'datacenter.fname', title: '数据中心名称',operate: false},
                            {
                                field: 'operate', title: __('Operate'), table: table1, events: Table.api.events.operate, buttons: [
                                    {
                                        name: 'forgid',
                                        title: '查看产品分组',
                                        text: '查看产品分组',
                                        icon: 'fa fa-list',
                                        classname: 'btn btn-primary btn-xs btn-click',
                                        click: function (e, data) {
                                            $("#myTabContent2 .form-commonsearch select[name='forgid']").val(data.id);
                                            $("#myTabContent2 .btn-refresh").trigger("click");
                                        }
                                    }
                                ], formatter: Table.api.formatter.operate
                            },

                        ]
                    ]
                });

                // 为表格1绑定事件
                Table.api.bindevent(table1);

            },
            second: function () {
                // 表格2
                var table2 = $("#table");
                var showExport  = $(".btn-export").data('value')

                table2.bootstrapTable({
                    url: 'bdl/bd/materialcate/index',
                    extend: {
                        index_url: "bdl/bd/materialcate/index",
                        export_url: 'bdl/bd/materialcate/export',
                        table: "bd_material_g"
                    },
                    toolbar: '#toolbar2',
                    sortName: 'id',
                    escape:false,
                    showExport: showExport,
                    showToggle: false,
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: 'ID',operate:false},
                            {field: 'fnumber', title: '编码', operate:false},
                            {field: 'fname', title: '名称', operate:false,align: 'left', formatter: Controller.api.formatter.title},
                            {
                                field: 'id',
                                title: "<a href=\"javascript:;\" class=\"btn btn-success btn-xs btn-toggle\" style=\"border-top:none;\"><i class=\"fa fa-chevron-up\"></i></a>",
                                operate: false,
                                formatter: Controller.api.formatter.subnode
                            } ,
                            {field: 'fenable', title: '使用状态', searchList: {"1":'可用',"0":'禁用'}, formatter: Table.api.formatter.status}  ,
                            {field: 'mar', title: '属性',operate:false,
                                formatter: function (value,row_data)
                                {
                                    var str = "";
                                    for (var i = 0; i < row_data.marpop.length; i++)
                                    {
                                        str += row_data.marpop[i].fname + "*";

                                    }

                                    return str;
                                }

                            },
                            {field: 'fk_bdl_comment', title: '备注',operate:false,visible:false},
                                {field: 'forgid', title: '组织ID',visible:false,searchList: $.getJSON("bdl/bd/datacenter/getFilter?type=get_org_list")},
                            {field: 'forg.fname', title: '组织名称',operate: false},


                            {field: 'fk_bdl_creatorid', title: '创建人',visible:false,searchList: $.getJSON("bdl/bd/datacenter/getFilter?type=get_admin_list")},
                            {field: 'creator.nickname', title: '创建人', operate: false},
                            {field: 'fk_bdl_createtime', title: '创建时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                            {field: 'fk_bdl_fmodifierid', title: '修改人',visible:false,searchList: $.getJSON("bdl/bd/datacenter/getFilter?type=get_admin_list")},
                            {field: 'modifier.nickname', title: '修改人', operate: false},
                            {field: 'fk_bdl_modifytime', title: '修改时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false}
                        ]
                    ],
                    pagination: false,
                    search: false,
                    rowAttributes: function (row, index) {
                        if (this.totalRows > 500) {
                            return row.fk_bdl_super == 0 ? {} : {style: "display:none"};
                        }
                    }
                });

                // 为表格2绑定事件
                Table.api.bindevent(table2);
                //显示隐藏子节点
                $(document).on("click", ".btn-node-sub", function (e) {
                    var status = $(this).data("shown") ? true : false;
                    $("a.btn[data-pid='" + $(this).data("id") + "']").each(function () {
                        $(this).closest("tr").toggle(!status);
                    });
                    $(this).data("shown", !status);
                    return false;
                });

                //批量删除后的回调
                $(".toolbar > .btn-del,.toolbar .btn-more~ul>li>a").data("success", function (e) {
                    Fast.api.refreshmenu();
                });

                //展开隐藏一级
                $(document.body).on("click", ".btn-toggle", function (e) {
                    $("a.btn[data-id][data-pid][data-pid!=0].disabled").closest("tr").hide();
                    var that = this;
                    var show = $("i", that).hasClass("fa-chevron-down");
                    $("i", that).toggleClass("fa-chevron-down", !show);
                    $("i", that).toggleClass("fa-chevron-up", show);
                    $("a.btn[data-id][data-pid][data-pid!=0]").not('.disabled').closest("tr").toggle(show);
                    $(".btn-node-sub[data-pid=0]").data("shown", show);
                });

                //展开隐藏全部
                $(document.body).on("click", ".btn-toggle-all", function (e) {
                    var that = this;
                    var show = $("i", that).hasClass("fa-plus");
                    $("i", that).toggleClass("fa-plus", !show);
                    $("i", that).toggleClass("fa-minus", show);
                    $(".btn-node-sub.disabled").closest("tr").toggle(show);
                    $(".btn-node-sub").data("shown", show);
                });

                $(document).on("click", ".distribution", function (e) {

                    var temp=$('#table').bootstrapTable('getAllSelections');


                    if(temp.length == 0)
                    {
                        layer.msg('请选择分类')
                    }else{

                        if(temp.length > 1){
                            layer.msg('最多只能选一个')
                        }else{
                            Fast.api.open("bdl/bd//materialcate/distribution?temp="+temp[0].id, "属性", {
                                callback: function (value)
                                {

                                }
                            });
                        }

                    }
                })

            },


        },

        distribution:function(){
            Controller.api.bindevent();
            Form.api.bindevent($("form[role=form]"));
        },
        api: {
            formatter: {
                title: function (value, row, index) {
                    value = value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;');
                    return !row.ismenu || row.status == 'hidden' ? "<span class='text-muted'>" + value + "</span>" : value;
                },
                name: function (value, row, index) {
                    return !row.ismenu || row.status == 'hidden' ? "<span class='text-muted'>" + value + "</span>" : value;
                },
                icon: function (value, row, index) {
                    return '<span class="' + (!row.ismenu || row.status == 'hidden' ? 'text-muted' : '') + '"><i class="' + value + '"></i></span>';
                },
                subnode: function (value, row, index) {
                    return '<a href="javascript:;" data-toggle="tooltip" title="' + __('') + '" data-id="' + row.id + '" data-pid="' + row.fk_bdl_super + '" class="btn btn-xs '
                        + (row.haschild == 1 || row.ismenu == 1 ? 'btn-success' : 'btn-default disabled') + ' btn-node-sub"><i class="fa fa-sitemap"></i></a>';
                }
            },
            bindevent: function () {

            }
        }



    };


    return Controller;
});
